<?php

/**
 * Class for viewing an input element in a form of type "text", "password", "file" or "hidden".
 *
 * @author Ian Clarence <ian.clarence@gmail.com>
 */
class ViewInputText extends ViewHtmlFormElement {

    /**
     * The default size for input elements.
     */
    const DEFAULT_SIZE = 40;

    /**
     * The default maximum length for input elements.
     */
    const DEFAULT_MAXLENGTH = 250;

    /**
     * The constructor.
     *
     * @param array $fields
     */
    public function __construct(array $fields = array()) {

        $fields["tag"] = "input";
        $fields["type"] = isset($fields["type"]) ? $fields["type"] : "text";        

        parent::__construct($fields);
        
        $this->setAttribute("type", $this->getType());

        if ($this->getType() != "hidden") {
            if ($this->getAttribute("size") == NULL) {
                $this->setAttribute("size", self::DEFAULT_SIZE);
            }
            if ($this->getAttribute("maxlength") == NULL && $this->getType() != "hidden") {
                $this->setAttribute("maxlength", self::DEFAULT_MAXLENGTH);
            }
        }
        
        if ($this->getValue() != NULL) {
            $this->setAttribute('value', $this->getValue());
        }
    }
    
    /**
     * Returns a ViewInputText object.
     *
     * @param string $elementId
     * @param string $name
     * @param string $type
     * @param string $value
     * @param int $size
     * @param int $maxlength
     * @return \self 
     */
    public static function inputTextInit(
        $elementId, 
        $name, 
        $type = 'text', 
        $value = NULL, 
        $size = self::DEFAULT_SIZE, 
        $maxlength = self::DEFAULT_MAXLENGTH
    ) {
        return new self(
            array(
                'element_id' => $elementId,
                'name' => $name,
                'type' => $type,
                'value' => $value,
                'size' => $size,
                'maxlength' => $maxlength
            )
        );
    }

}